//菜单编辑
var groupid;
var iconid;
var subid;
var menuid;
var index = "";
$(function() {
  groupid = ly.getUrlParam("groupid");
  iconid = ly.getUrlParam("iconid");
  subid = ly.getUrlParam("subid");
  menuid = ly.getUrlParam("menuid");
  //构建列表
  $("#menulist").getChannelList({
    "initData": {
      num: 1,
      size: '',
      id: ''
    },
    "url": site.apiUrl + "/back/menu/selectMenuListByPageByThis", // 数据请求url
    "type": "POST",
    "col": [ //数据列	     
      { "field": "menuName", "display": "菜单标题", "width": "2" },
      { "field": "menuCode", "display": "菜单编码", "width": "1" },
      { "field": "icon", "display": "菜单图标", "width": "2", "fun": "changePic" },
      { "field": "menuNameNext", "display": "父级", "width": "2", "fun": "changeMenuNameNext" },
      { "field": "parentNum", "display": "下级数量", "width": "1" },
      { "field": "nodeOrder", "display": "显示顺序", "width": "1" },
      // { "field": "clientTypeDisplay", "display": "客户端类型", "width": "1" },
      // { "field": "isUsingStr", "display": "状态" },
      {
        "field": "isUsing",
        "display": "状态",
        "show": [
          { "value": "0", "html": "<span class='red'>禁用</span>" },
          { "value": "1", "html": "<span class='green'>启用</span>" }
        ]
      }
    ],
    "option": [ //操作按钮配置
      { "icon": "add", "tip": "添加下级菜单", "fun": "addMenu('{{id}}')" }, //icon: view,edit,add,del
      /*{"icon":"copyasexam","tip":"添加下级功能","fun":"addNextFunction('{{id}}','{{parentNum}}','{{menuName}}')"},*/ //icon: view,edit,add,del
      { "icon": "edit", "tip": "编辑", "fun": "editMenu('{{id}}')" }, // 花括号内可以对应相关字段
      { "icon": "del", "tip": "删除", "fun": "deleteMenu('{{id}}','{{parentNum}}')" },
    ]
  });

  var success = window.sessionStorage.getItem("deleteMenuByIdSuccess");
  if (success == 1) {
    ly.toast('删除成功');
    window.sessionStorage.removeItem("deleteMenuByIdSuccess");
  }

});

//编辑
function editMenu(id) {
  ly.goto("menu-edit.html?id=" + id + "&groupid=" + groupid + "&iconid=" + iconid + "&subid=" + subid + "&menuid=" + menuid);
}
//添加下级菜单
function addMenu(id) {
  ly.goto("menu-create.html?id=" + id + "&groupid=" + groupid + "&iconid=" + iconid + "&subid=" + subid + "&menuid=" + menuid);
}

//添加下级功能
function addNextFunction(id, parentNum, menuName) {
  if (parentNum != null && parentNum != '' && parentNum == '0') {
    ly.goto("set_menu_fun.html?id=" + id + "&menuName=" + menuName + "&groupid=" + groupid + "&iconid=" + iconid + "&subid=" + subid + "&menuid=" + menuid);
  } else {
    ly.toast("上级菜单不允许添加功能");
  }

}

//显示图片
function changePic(v, o, d) {
  $(o).html("<i class='iconfont " + d.icon + "'></i>");
}

//下级数量
function changeMenuNameNext(v, o, d) {
  // $(o).html("<img src='"+$($(o).html()).html()+"' width='30' height='30'>");
  var i = d.menuNameNext;
  if (i != null && i != '' && i != 'undefined') {
    $(o).html("<i class='lm-expand pl10'>" + i + "</i>")
  } else {
    $(o).html("<i class='lm-expand pl10'>顶级菜单</i>")
  }

}

//删除
function deleteMenu(id, num) {
  var deleteUserId = window.localStorage.getItem("backstage-access-id");
  var obj = {
    'id': id,
    'deleteUserId': deleteUserId
  };
  var url = site.apiUrl + "/back/menu/deleteMenuByThis";
  ly.confirm("你确定删除吗？", function() {
    //post(url,obj,deleteCallback,false,false);
    request({
      url: url,
      data: obj,
      type: "post",
      callback: function(f) {
        console.log(f);
        deleteCallback(f);
      }
    });

  });
}

function deleteCallback(data) {
  console.log(data);
  if (data.code == 200) {
    window.sessionStorage.setItem("deleteMenuByIdSuccess", "1");
    window.location.reload();
  } else {
    ly.toast(data.desc || data.message);
  }
}